package com.lld.crowdfunding.controller;

import com.lld.crowdfunding.entity.User;
import com.lld.crowdfunding.service.UserService;
import com.lld.crowdfunding.util.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@Api(tags = "用户相关操作接口")
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("实验获取所有用户信息")
    @GetMapping("/all")
    @ResponseBody
    public Msg getAll() {
        return userService.getAll();
    }


    @ApiOperation("处理登录请求")
    @PostMapping("/doLogin.html")
    public String doLogin(@RequestParam("loginName") String loginName,
                          @RequestParam("userPwd") String userPwd, HttpSession session,
                          Model model, HttpServletRequest request, HttpServletResponse response) {
        Msg msg = userService.login(loginName, userPwd,session,request,response);
        if (msg.getStatus() == 200) {
            return "redirect:/";
        } else {
            model.addAttribute("fail", msg.getExtend().get("fail"));
            return "login";
        }
    }


    @ApiOperation("退出登录")
    @GetMapping("/logout.html")
    public String logout(HttpSession session,HttpServletResponse response,HttpServletRequest request) {
        session.removeAttribute("user");
        Cookie cookie = new Cookie("cookie_username","");
        cookie.setMaxAge(60*60*24);//设置为0即删除cookie
        cookie.setPath("/");
        response.addCookie(cookie);
        return "redirect:/login.html";
    }


    @ApiOperation("新增界面跳转")
    @GetMapping("/addUser.html")
    public String openAddUser() {
        return "addUser";
    }

    @ApiOperation("新增用户请求")
    @PostMapping("/user")
    public String addUser(User user, MultipartFile file) throws IOException {
        userService.saveUser(user,file);
        return "redirect:/userManager.html";
    }

    @ApiOperation("删除用户请求")
    @GetMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id) {
        Msg msg = userService.deleteUser(id);
        return "redirect:/userManager.html";
    }

    @ApiOperation("修改界面跳转")
    @GetMapping("/updateUser.html/{id}")
    public String openUpdate(@PathVariable("id") Long id,Model model) {
        Msg msg = userService.getUserById(id);
        model.addAttribute("updateUser",msg.getExtend().get("user"));
        return "updateUser";
    }

    @ApiOperation("修改用户请求")
    @PostMapping("/update")
    public String updateUser(User user, MultipartFile file) throws IOException {
        userService.updateUser(user,file);
        return "redirect:/userManager.html";
    }
}
